package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import eu.bitwalker.useragentutils.UserAgent;
import io.renren.modules.generator.entity.EcharsData;
import io.renren.modules.generator.entity.VisitNum;
import io.renren.modules.oss.utils.AddressUtils;
import io.renren.modules.oss.utils.IpUtils;
import io.renren.modules.oss.utils.ServletUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.SysLogininforEntity;
import io.renren.modules.generator.service.SysLogininforService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 系统访问记录
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-28 16:55:56
 */
@RestController
@RequestMapping("generator/syslogininfor")
public class SysLogininforController {
    @Autowired
    private SysLogininforService sysLogininforService;

    /**
     * 查询总访问人数和今天的访问人数
     */
    @RequestMapping("/getVisitNum")
    @RequiresPermissions("generator:syslogininfor:list")
    public R getVisitNum(@RequestParam Map<String, Object> params){
//        PageUtils page = sysLogininforService.queryPage(params);
       VisitNum visitNum = sysLogininforService.getVisitNum();
        return R.ok().put("visitNum", visitNum);
    }

    /**
     * 年度访问人数统计表（1-12月人数）
     */
    @RequestMapping("/everyMonthData")
    @RequiresPermissions("generator:syslogininfor:list")
    public R everyMonthData(@RequestParam Map<String, Object> params){
//        PageUtils page = sysLogininforService.queryPage(params);
        int[] data = sysLogininforService.selectVisitorsOfMonth((String) params.get("yearNum"));
        return R.ok().put("data", data);
    }
    /**
     * 访问人的省份占比
     */
    @RequestMapping("/provinceNum")
    @RequiresPermissions("generator:syslogininfor:list")
    public R provinceNum(@RequestParam Map<String, Object> params){
//        PageUtils page = sysLogininforService.queryPage(params);
        List<EcharsData> provinceNumData = sysLogininforService.selectProvinceAndNum();
//        for (EcharsData provinceNumDatum : provinceNumData) {
//            String seriesData = provinceNumDatum.getSeriesData();
//            String xAxisData = provinceNumDatum.getXAxisData();
//            if(!StringUtils.isBlank(seriesData)){
//             seriesData=seriesData.substring(0,2);
//            }
//            if(!StringUtils.isEmpty(xAxisData)){
//                xAxisData=xAxisData.substring(0,2);
//            }
//            provinceNumDatum.setSeriesData(seriesData);
//            provinceNumDatum.setXAxisData(xAxisData);
//        }

        return R.ok().put("provinceNumData", provinceNumData);
    }
    /**
     * 保存访问日志
     */
    @RequestMapping("/saveLogininfo")
//    @RequiresPermissions("generator:syslogininfor:save")
    public R saveLogininfo(){
        SysLogininforEntity sysLogininforEntity = new SysLogininforEntity();
        sysLogininforEntity.setUserName("访问用户");
        HttpServletRequest request = ServletUtils.getRequest();
        String remoteAddr = request.getRemoteAddr();
        String ipAddr = IpUtils.getIpAddr(request);
        sysLogininforEntity.setIpaddr(ipAddr);
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        String realAddressByIP = AddressUtils.getRealAddressByIP(ipAddr);
        sysLogininforEntity.setLoginLocation(realAddressByIP);
        sysLogininforEntity.setBrowser(browser);
        sysLogininforEntity.setOs(os);
        sysLogininforEntity.setType(String.valueOf(1));
        sysLogininforEntity.setStatus(String.valueOf(0));
        sysLogininforEntity.setMsg("访问成功");
        sysLogininforEntity.setLoginTime(new Date());
        sysLogininforService.save(sysLogininforEntity);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:syslogininfor:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysLogininforService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{infoId}")
    @RequiresPermissions("generator:syslogininfor:info")
    public R info(@PathVariable("infoId") Long infoId){
		SysLogininforEntity sysLogininfor = sysLogininforService.getById(infoId);

        return R.ok().put("sysLogininfor", sysLogininfor);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:syslogininfor:save")
    public R save(@RequestBody SysLogininforEntity sysLogininfor){
		sysLogininforService.save(sysLogininfor);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:syslogininfor:update")
    public R update(@RequestBody SysLogininforEntity sysLogininfor){
		sysLogininforService.updateById(sysLogininfor);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:syslogininfor:delete")
    public R delete(@RequestBody Long[] infoIds){
		sysLogininforService.removeByIds(Arrays.asList(infoIds));

        return R.ok();
    }

}
